// outlaystreamApi.ts
import Request from "@/api/request";
import store from "@/store";
// in .ts file use this router; in .vue use useRouter()
import { OutlayStream } from "@/interface/entity";
import {
  generateOutlayStreamLists,
  processOutlayStreamBeforeToDb
} from "@/functions/outlaystream";

export async function addOutLayStreamToDb(outlayStream: OutlayStream) {
  const stream = processOutlayStreamBeforeToDb(outlayStream, "add");
  await Request({
    method: "post",
    url: "OutlayStream/AddOutlayStream",
    data: JSON.stringify(stream)
  });
}
export async function deleteOutlayStreamInDb(outlayStream: OutlayStream) {
  await Request({
    method: "delete",
    url: "/OutlayStream/DeleteOutlayStream",
    params: {
      streamID: outlayStream.streamID
    }
  });
}
export async function updateOutlayStreamToDb(outlayStream: OutlayStream) {
  const stream = processOutlayStreamBeforeToDb(outlayStream);
  await Request({
    method: "put",
    url: "OutlayStream/UpDateOutlayStream",
    data: JSON.stringify(stream)
  });
}
export async function queryAllOutlayStream() {
  await Request({
    method: "get",
    url: "OutlayStream/QueryAllOutlayStream"
  })
    .then(response => {
      // update store
      store.state.tableData = generateOutlayStreamLists(
        response.data as OutlayStream[]
      );
    })
    .catch(() => {
      store.state.tableData = [] as OutlayStream[];
    });
}
